if (g_uname_minlength == null){
	var g_uname_minlength = 3;
	var g_uname_maxlength = 15;
}else{
	g_uname_minlength = g_uname_minlength ||3;
	g_uname_maxlength = g_uname_maxlength ||15;
}

function getHDUrl(url){
	return g_seo_prefix + url + g_seo_suffix;
}

function strlen(str){
 var len=0;
 for(var i=0;i<str.length;i++){
 	if(str.charCodeAt(i)>127){
 		len++;
 	}
 	len++;
 }
 return len;
}
 
function getAction(url){
	var auth, re;
	re = /[a-z]+-[a-z]+/;
	auth = url.match(re);
	if (auth){
		auth = auth[0];
	} else {
		auth = '';
	}
	
	return auth;
}

function doLogin(E){
	var auth;
	if (E){
		g_forward = E.href;
	} else {
		g_forward = '';
	}
	
	if (g_regulars && g_forward){
		auth = getAction(g_forward);
		if (g_regulars.indexOf(auth) != -1){
			return true;
		}
	}
	
	if (g_isLogin === false){
		if(g_isApi){
			window.location.href=api_url;
		}
		$.dialog.box('login', Lang.Login, 'url:'+ getHDUrl('user-boxlogin'));
		return false;
	}else {
		return true;
	}
}

var Message = {
	sendto: '',
	box : function(username){
		this.sendto = username;
		if (doLogin()){
			var html = '<table border="0" width="400" class="send_massage"><tr><td width="60" >'+Lang.Subject+'</td>'
			+'<td><input id="messageSubject" type="text" class="inp_txt" maxlength="35"/></td></tr><tr><td>'+Lang.Content+'</td>'
			+'<td><textarea id="messageContent" cols="47" rows="6" class="area" style="width:312px"></textarea><br />'+Lang.TipMessageLength+'</td></tr>'
			+'<tr><td></td><td height="40"><input id="messageSubmit" onclick="Message.send()" type="submit" value="'+Lang.Submit+'" />'
			+'&nbsp;&nbsp;<span id="messageTip"></span></td></tr></table>';
			
			$.dialog.box('login', Lang.sendMessage + Lang.To + ' ' +username, html);
			
			$("#messageSubject").val('');
			$("#messageContent").val('');
			$("#messageSubject").focus();
			$("#messageSubmit").attr('disabled', false).val(Lang.Submit);
		}
		return false;
	},
	
	send: function(){
		var params = {'submit':'ajax', 'checkbox':0, 'sendto':this.sendto};
		params.subject = $("#messageSubject").val();
		params.content = $("#messageContent").val();
		
		params.subject = $.trim(params.subject);
		params.content = $.trim(params.content);		
		
		params.content = params.content.substr(0,300);
		
		if (params.subject == ''){
			$("#messageSubject").focus();
			$("#messageTip").css('color','red').html(Lang.TipMessageSubjectIsNull);
			return false;
		}
		
		if (params.content == ''){
			$("#messageContent").focus();
			$("#messageTip").css('color','red').html(Lang.TipMessageContentIsNull);
			return false;
		}
		$("#messageSubmit").attr('disabled', true).val(Lang.Submiting);

		$.post(getHDUrl("pms-sendmessage"), params, function(data, status){
			$("#messageSubmit").attr('disabled', false).val(Lang.Submit);
			if ( status == 'success'){
				if (data == 'OK'){
					//send success
					alert(Lang.TipMessageSendOk);
					$("#messageTip").html('');
					$.dialog.close('login');
				} else {
					//send false
					alert(Lang.TipMessageSendError);
				}
			} else {
				alert(Lang.TipMessageSendError);
			}
		});
	}
	
	
}

var boxLogin = {
	Err: null,
	Form: null,
	
	init: function(){
		this.Err = $('#box-login-error');
		this.Form = $("form[name='box-login']");
	},
	
	checkUserName: function(){
		var err = $('#box-login-error').css('color','#FF0000');
		var username = $("#box-login-username").val();
		if (strlen(username) < g_uname_minlength){
			Lang.TipUserName = Lang.TipUserName.replace('3',g_uname_minlength).replace('15',g_uname_maxlength);
			err.html(Lang.TipUserName);
			return false;
		} else {
			var params = {username: username};
			$.post(getHDUrl("user-checkusername"), params, function(data, status){
				if ( status == 'success'){
					var isOK = data.lastChild.firstChild.nodeValue;
					if (isOK == 'OK'){//not exist
						err.html(Lang.TipUserNotExist);
					} else {
						err.html("");
					}
				} else {
					//net link error
				}
			});
		}
	},
	
	checkPsssword: function(){
		var err = $('#box-login-error').css('color','#FF0000');
		var pwd = $("#box-login-password").val();
		
		if (pwd.length == 0){
			err.html(Lang.TipPassword);
			return false;
		} else {
			err.html("");
		}
	},
	
	checkCode:function(){
		var err = $('#box-login-error').css('color','#FF0000');
		var code = $("#box-login-code").val();
		if (code.length < 4){
			err.html(Lang.TipCode);
			return false;
		} else {
			var params = {code: code};
			$.post(getHDUrl("user-checkcode"), params, function(data, status){
				if ( status == 'success'){
					var isOK = data.lastChild.firstChild.nodeValue;
					if (isOK == 'OK'){
						err.html("");
					} else {
						err.html(Lang.TipCode);
					}
				} else {
					//net link error
				}
			});
		}
	},
	
	go: function(){
		if (g_forward){
			location.href =g_forward;
		} else {
			location.reload();
		}	
	},
	
	check: function(){
		var err = $('#box-login-error').css('color','#FF0000');
		var username = $("#box-login-username").val();
		var password = $("#box-login-password").val();
		var code = $("#box-login-code");
		if (code.length > 0) code = code.val();
		else code = '****';
		
		if (strlen(username) < g_uname_minlength){err.html(Lang.TipUserName);return false;}
		if (0 == password.length){err.html(Lang.TipPassword);return false;}
		if (code.length < 4){err.html(Lang.TipCode);return false;}
		
		url = $("form[name=box-login]").attr('action');
		
		var params = {username: username, password: password, submit:'ajax'};
		if (code != '****'){
			params.code = code;
		}

		$("form[name=box-login]").find('input[type=submit]').attr('disabled',true).val(Lang.Submiting);
		$.post(url, params, function(data, status){
			if ( status == 'success'){
				data = $.trim(data);
				var isOK = data;
				if (isOK == 'OK'|| isOK == ''){
					boxLogin.go();
				} else {
					if (data.substr(0,7) == '<script'){
						window.onerror = function(){return false};
						var jsUrl = data.match(/http:[^ '"]*/ig);
						if (jsUrl != null){
							$("form[name=box-login]").find('input[type=submit]').val(Lang.TipUcenterLogin);
							for(i in jsUrl){
								if (isNaN(parseInt(i))) continue;
								var script=document.createElement("script");
								script.setAttribute("type","text/javascript");
								script.setAttribute("src",jsUrl[i]);
								$("head").append(script);
							}
							setTimeout("boxLogin.go()", 3000);
						} else {
							alert(data);
						}
						window.onerror = function(){return true};
						
					}else if (data.substr(0,9) == '<!DOCTYPE'){
						var tip = data.match(/<dd>([^<]+)<br\/>/i);
						if (tip && tip[1] && tip[1].indexOf(Lang.Success) == -1){
							err.text(tip[1]);
							$("form[name=box-login]").find('input[type=submit]').attr('disabled',false).val(Lang.Submit);
						}else{
							boxLogin.go();
						}
					}else {
						err.text(data.substr(0,18));
						$("form[name=box-login]").find('input[type=submit]').attr('disabled',false).val(Lang.Submit);
					}
				}
			} else {
				boxLogin.go();
				//net link error
			}
		});
		
		
		return false;
	}
}

function updateverifycode(){
	$('#verifycode').attr('src', getHDUrl("user-code-"+Math.random()));
}